package com.shijie.controller.app;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.component.AppUserService;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.domain.entity.UserComments;
import com.shijie.domain.enums.EnumInterface.EntityType;
import com.shijie.service.UserCommentsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/*
 *  用户评论等相关接口
 *
 *
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/app/comments")
public class AppUserCommentsController {

    @Resource
    private UserCommentsService userCommentsService;

    @Resource
    private AppUserService appUserService;

    @GetMapping
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer pageSize, Integer entityId, EntityType entityType) {
        PageHelper.startPage(page, pageSize);
        List<UserComments> list = userCommentsService.queryCommentsByIdAndType(entityId, entityType);
        PageInfo<UserComments> pageInfo = new PageInfo<>(list);
        return ResultGenerator.success(pageInfo);
    }

    /**
     * 增加留言 评论
     *
     * @param userComments
     *          entityType 0资讯；1活动；2达人 ；3商品 ；4 内容（达人）
     *          entityId
     * @return
     */
    @PostMapping
    public Result add(@RequestBody UserComments userComments) {
        if (userComments.getEntityType() == null) {
            return ResultGenerator.error("评论类型 entityType 必须维护！");
        }
        if (userComments.getEntityId() == null) {
            return ResultGenerator.error("评论类型对应 entityId 必须维护！");
        }
        if (userComments.getContent() == null) {
            return ResultGenerator.error("评论类容必须维护！");
        }

        userComments.setFromId(appUserService.getCurrentUser().getId());
        userComments.setFromName(appUserService.getUserName());
        userComments.setFromImg(appUserService.getCurrentUser().getAvatar());
        userComments.setCommentTime(new Date());
        userComments.setCreateBy(appUserService.getUserName());
        userComments.setCreateTime(new Date());
        userCommentsService.save(userComments);
        return ResultGenerator.success(userComments);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        userCommentsService.deleteById(id);
        return ResultGenerator.success();
    }
}

